package com.eco.fanliapp.rocketmq;


import com.aliyun.mq.http.MQClient;
import com.aliyun.mq.http.MQProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


/**
 * rocketmq生产者启动初始化类
 *
 */
@Component
public class RocketmqProducerInit {

    private static final Logger log = LoggerFactory.getLogger(RocketmqProducerInit.class);

    private static MQProducer producer;

    @PostConstruct
    public void init() {
        MQClient mqClient=new MQClient(RocketConfig.NAMESRV_ADDR,RocketConfig.AccessKey,RocketConfig.SecretKey);
        if (RocketConfig.INSTANCEID != null && RocketConfig.INSTANCEID != "") {
            producer = mqClient.getProducer(RocketConfig.INSTANCEID, RocketConfig.TOPIC);
        } else {
            producer = mqClient.getProducer(RocketConfig.TOPIC);
        }
        // 在发送消息前，初始化调用start方法来启动Producer，只需调用一次即可，当项目关闭时，自动shutdown
        log.info("producer is start !!! GROUP_ID:{},TOPIC:{},NAMESRV_ADDR:{}", RocketConfig.GROUP_ID, RocketConfig.TOPIC, RocketConfig.NAMESRV_ADDR);
    }
    /**
     * 初始化生产者
     *
     * @return
     */
    public MQProducer getProducer() {
        return producer;
    }

}
